﻿Imports System
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    Friend NotInheritable Class WebServiceReceiveValidator
        Inherits ActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim activity As WebServiceInputActivity = [TryCast](obj,WebServiceInputActivity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(WebServiceInputActivity).FullName }), "obj")
            End If
            If Not Helpers.IsActivityLocked(activity) Then
                Dim list As List([Of] ParameterInfo)
                Dim list2 As List([Of] ParameterInfo)
                If activity.IsActivating Then
                    If WebServiceActivityHelpers.GetPreceedingActivities(activity).GetEnumerator.MoveNext Then
                        Dim item As New ValidationError(SR.GetString("Error_ActivationActivityNotFirst"), &H568) { _
                            .PropertyName = "IsActivating" _
                        }
                        errors.Add(item)
                        Return errors
                    End If
                    If WebServiceActivityHelpers.IsInsideLoop(activity, Nothing) Then
                        Dim error2 As New ValidationError(SR.GetString("Error_ActivationActivityInsideLoop"), &H579) { _
                            .PropertyName = "IsActivating" _
                        }
                        errors.Add(error2)
                        Return errors
                    End If
                ElseIf Not WebServiceActivityHelpers.GetPreceedingActivities(activity, True).GetEnumerator.MoveNext Then
                    Dim error3 As New ValidationError(SR.GetString("Error_WebServiceReceiveNotMarkedActivate"), &H569) { _
                        .PropertyName = "IsActivating" _
                    }
                    errors.Add(error3)
                    Return errors
                End If
                Dim service As ITypeProvider = DirectCast(manager.GetService(GetType(ITypeProvider)), ITypeProvider)
                If (service Is Nothing) Then
                    Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
                End If
                Dim interfaceType As Type = Nothing
                If (Not activity.InterfaceType Is Nothing) Then
                    interfaceType = service.GetType(activity.InterfaceType.AssemblyQualifiedName)
                End If
                If (interfaceType Is Nothing) Then
                    Dim error4 As New ValidationError(SR.GetString("Error_TypePropertyInvalid", New Object() { "InterfaceType" }), &H116) { _
                        .PropertyName = "InterfaceType" _
                    }
                    errors.Add(error4)
                    Return errors
                End If
                If Not interfaceType.IsInterface Then
                    Dim error5 As New ValidationError(SR.GetString("Error_InterfaceTypeNotInterface", New Object() { "InterfaceType" }), &H570) { _
                        .PropertyName = "InterfaceType" _
                    }
                    errors.Add(error5)
                    Return errors
                End If
                If String.IsNullOrEmpty(activity.MethodName) Then
                    errors.Add(ValidationError.GetNotSetValidationError("MethodName"))
                    Return errors
                End If
                Dim interfaceMethod As MethodInfo = Helpers.GetInterfaceMethod(interfaceType, activity.MethodName)
                If (interfaceMethod Is Nothing) Then
                    Dim error6 As New ValidationError(SR.GetString("Error_MethodNotExists", New Object() { "MethodName", activity.MethodName }), &H137) { _
                        .PropertyName = "MethodName" _
                    }
                    errors.Add(error6)
                    Return errors
                End If
                Dim errors2 As ValidationErrorCollection = WebServiceActivityHelpers.ValidateParameterTypes(interfaceMethod)
                If (errors2.Count > 0) Then
                    Dim error7 As ValidationError
                    For Each error7 In errors2
                        error7.PropertyName = "MethodName"
                    Next
                    errors.AddRange(errors2)
                    Return errors
                End If
                WebServiceActivityHelpers.GetParameterInfo(interfaceMethod, list, list2)
                Dim info2 As ParameterInfo
                For Each info2 In list
                    Dim name As String = info2.Name
                    Dim parameterPropertyName As String = ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(activity.GetType, name)
                    Dim type2 As Type = If(info2.ParameterType.IsByRef, info2.ParameterType.GetElementType, info2.ParameterType)
                    Dim binding As Object = Nothing
                    If activity.ParameterBindings.Contains(name) Then
                        If activity.ParameterBindings.Item(name).IsBindingSet(WorkflowParameterBinding.ValueProperty) Then
                            binding = activity.ParameterBindings.Item(name).GetBinding(WorkflowParameterBinding.ValueProperty)
                        Else
                            binding = activity.ParameterBindings.Item(name).GetValue(WorkflowParameterBinding.ValueProperty)
                        End If
                    End If
                    If (Not type2.IsPublic OrElse Not type2.IsSerializable) Then
                        Dim error8 As New ValidationError(SR.GetString("Error_TypeNotPublicSerializable", New Object() { name, type2.FullName }), &H567) { _
                            .PropertyName = parameterPropertyName _
                        }
                        errors.Add(error8)
                    Else
                        If (Not activity.ParameterBindings.Contains(name) OrElse (binding Is Nothing)) Then
                            Dim notSetValidationError As ValidationError = ValidationError.GetNotSetValidationError(name)
                            notSetValidationError.PropertyName = parameterPropertyName
                            errors.Add(notSetValidationError)
                            [Continue] For
                        End If
                        Dim read As AccessTypes = AccessTypes.Read
                        If info2.ParameterType.IsByRef Then
                            read = (read Or AccessTypes.Write)
                        End If
                        Dim errors3 As ValidationErrorCollection = ValidationHelpers.ValidateProperty(manager, activity, binding, New PropertyValidationContext(activity.ParameterBindings.Item(name), Nothing, name), New BindValidationContext(If(info2.ParameterType.IsByRef, info2.ParameterType.GetElementType, info2.ParameterType), read))
                        Dim error10 As ValidationError
                        For Each error10 In errors3
                            error10.PropertyName = parameterPropertyName
                        Next
                        errors.AddRange(errors3)
                    End If
                Next
                If (activity.ParameterBindings.Count > list.Count) Then
                    errors.Add(New ValidationError(SR.GetString("Warning_AdditionalBindingsFound"), &H630, True))
                End If
                Dim flag As Boolean = False
                Dim activity2 As Activity
                For Each activity2 In WebServiceActivityHelpers.GetSucceedingActivities(activity)
                    If ((Not TypeOf activity2 Is WebServiceOutputActivity OrElse (DirectCast(activity2, WebServiceOutputActivity).InputActivityName <> activity.Name)) AndAlso (Not TypeOf activity2 Is WebServiceFaultActivity OrElse Not (DirectCast(activity2, WebServiceFaultActivity).InputActivityName = activity.Name))) Then
                        [Continue] For
                    End If
                    flag = True
                    Exit For
                Next
                If (((list2.Count > 0) OrElse (Not interfaceMethod.ReturnType Is GetType(Void))) AndAlso Not flag) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceResponseNotFound"), &H55D))
                End If
            End If
            Return errors
        End Function

    End Class
End Namespace

